package com.blb.springsecurity.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

/**
 * @author lvtian
 * @date 2023/1/31 9:53
 * springsecurity的配置
 */
//启动security的验证
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //提供密码编码器
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private UserDetailsService userDetailsService;
    //注入jdbc的操作对象
    @Autowired
    private PersistentTokenRepository persistentTokenRepository;

    //配置验证用户的账号和密码
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //在内存中新建用户
//        auth.inMemoryAuthentication()
//                .withUser("cyh")
//                .password(bCryptPasswordEncoder.encode("123"))
//                .roles("ADMIN","USER") //角色
//                .authorities("SHOW_ADMIN")
//                .and()
//                .withUser("lt")
//                .password(bCryptPasswordEncoder.encode("123"))
//                .roles(("USER"))//角色
//                .authorities("SHOW_USER"); //权限
        auth.userDetailsService(userDetailsService);
    }

    //配置访问控制
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //给请求授权
        http.authorizeRequests()
                //给登录相关的请求放行
                .antMatchers("/login","/login.html","/**/*.css").permitAll()
                //.antMatchers("/admin/**").hasRole("ADMIN")
                 .antMatchers("/admin/**").hasAuthority("销售管理")
                //.antMatchers("/user/**").hasRole("USER")
                .antMatchers("/user/**").hasAuthority("采购管理")
                //其余的都拦截
                .anyRequest().authenticated()
                .and()
                //配置自定义登录
                .formLogin()
                .loginPage("/login.html") //登录页面
                .loginProcessingUrl("/login") //处理登录的url
                .successForwardUrl("/hello.html") //登录成功后跳转的url
        ;

                http
        //配置记住我
                .rememberMe()
        //表单中的名称
                .rememberMeParameter("rememberMe")
        //jdbc操作对象
                .tokenRepository(persistentTokenRepository)
        //记住我的时间为60秒
                .tokenValiditySeconds(60);

    }
}
